package com.cmwa.solution.business.product.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.product.entity.TreFeeTypeVo;
import com.cmwa.solution.business.product.service.TreFeeTypeService;
import com.cmwa.solution.exception.ValidateFailedException;
import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.utils.cached.DictionaryCached;

import net.sf.json.JSONArray;

/**
 * 费用类型管理控制器
 * 
 * @author ex-chenxh
 * @date 2019-01-22 10:30:12
 */
@Controller
@RequestMapping("/business/treFeeType")
public class TreFeeTypeController extends BaseController {
	
	private static final Log log = LogFactory.getLog(TreFeeTypeController.class);

	private final static String JSP_PATH = "business/product/";
	@Resource
	private TreFeeTypeService treFeeTypeService;
		
	/**
	 * 跳转到列表页面
	 * @author ex-chenxh
	 * @date 2019-01-22 10:30:12
	 * @param model
	 * @return
	 */
	@RequestMapping("/treFeeTypeListView")
	public String treFeeTypeListView(ModelMap model) {
		log.info("前往费用类型管理列表页面");
		return JSP_PATH + "treFeeTypeList";
	}
	
	/**
	 * 通过条件查询数据并分页
	 * @param param
	 * @return
	 */
	@RequestMapping("/treFeeTypeListPage")
	@ResponseBody
	public Page<TreFeeTypeVo> treFeeTypeListPage(SearchParam param) {
		log.info("费用类型管理列表页面加载分页数据");
		return treFeeTypeService.treFeeTypeListPage(param);
	}
	/**
	 * 前往费用类型新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/treFeeTypeView")
	public String treFeeTypeView(String actionType,String feeCode,ModelMap model) throws ValidateFailedException {
		log.info("前往费用类型编辑页面，当前操作类型为：" + actionType);
		List<DictionaryVo> dictionaryList = null;
		TreFeeTypeVo treFeeTypeVo = null;
		
		//执行动作不为空 并且 不等于add类型的情况，需要根据ID加载费用类型信息
		if(StringUtils.isNotBlank(actionType) && !"add".equals(actionType)) {
			//加载费用类型信息
			treFeeTypeVo = treFeeTypeService.getTreFeeTypeByFeeCode(feeCode);
			//如果费用类型信息不为空，则加载附件信息
			if(null == treFeeTypeVo) {
				log.error("根据前端传递过来的主键：" + feeCode + ",没有获取到相关数据");
				throw new ValidateFailedException("根据前端传递过来的信息,没有获取到相关数据");
			}
		}
		//加载拓展字段下拉选项
		dictionaryList = DictionaryCached.getDictionaryByType("TreFeeType_TYPE");
		//设置前端要显示的数据
		model.addAttribute("treFeeTypeVo", treFeeTypeVo);
		model.addAttribute("actionType", actionType);
		model.addAttribute("dictionaryList", dictionaryList);
		return JSP_PATH + "treFeeTypeView";
	}
	
	/**
	 * 新增费用类型信息
	 * @param TreFeeTypeVo
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveTreFeeType",method = {RequestMethod.POST})
	public String saveTreFeeType(TreFeeTypeVo treFeeDetailVo,ModelMap model) throws ValidateFailedException {
		log.info("新增费用类型信息" );
		//前台传递过来的对象不能为空
		if (null != treFeeDetailVo) {
			String feeCode = treFeeDetailVo.getFeeCode();
			TreFeeTypeVo treFeeDetail = treFeeTypeService.getTreFeeTypeByFeeCode(feeCode);
			
			//判断该feeCode是否已经存在数据库中，如果已存在则不允许新增，并返回错误提示
			if(null != treFeeDetail) {
				log.error("新增失败，该feeCode是否已经存在数据库中:"+ feeCode);
				model.addAttribute("exception", "新增失败，该费用编码已经存在数据库中:"+ feeCode);
				return redirectExecFaild();
			}
			//将费用类型存入数据库中
			treFeeTypeService.insertTreFeeType(treFeeDetailVo);
			return redirectSuccess();
		}else {
			log.error("新增失败，参数treFeeDetailVo不能为空:"+treFeeDetailVo);
			model.addAttribute("exception", "新增失败，参数不能为空");
			return redirectExecFaild();
		}
	}
	/**
	 * 修改费用类型信息
	 * @param TreFeeTypeVo
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/updateTreFeeType",method = {RequestMethod.POST})
	public String updateTreFeeType(TreFeeTypeVo treFeeDetailVo,ModelMap model) throws Exception {
		log.info("修改费用类型信息" );
		//前台传递过来的参数不能为空
		if (null != treFeeDetailVo) {
			//将费用类型存入数据库中
			treFeeTypeService.updateTreFeeType(treFeeDetailVo);
			return redirectSuccess();
		}else {
			log.error("修改失败，参数不能为空");
			model.addAttribute("exception", "修改失败，参数不能为空");
			return redirectExecFaild();
		}
	}
	/**
	 * 删除费用类型信息
	 * @param TreFeeTypeVo
	 * @param model
	 * @return
	 * @throws ValidateFailedException
	 */
	@RequestMapping("/deleteTreFeeType")
	@ResponseBody
	public Result<Object> deleteTreFeeType(@RequestParam("feeCode") String feeCode,ModelMap model) throws ValidateFailedException {
		log.info("根据主键删除费用类型信息，feeCode：" + feeCode );
		
		Result<Object> result = new Result<Object>();
		boolean isComplete = false;
		//判断需要删除的主键是否为空
		if(StringUtils.isNotBlank(feeCode)) {
			int status = treFeeTypeService.deleteTreFeeType(feeCode);
			log.info("根据主键删除相关记录数：" + status);
			if(status > 0) {
				isComplete = true;
			}
		}else {
			log.error("需要删除的id不能为空！");
			result.setMessage("需要删除的主键不能为空！");
		}
		result.setSuccess(isComplete);
		return result;
	}
	
	@RequestMapping(value="/isExist",method=RequestMethod.POST)
	@ResponseBody
	public String isExist(@RequestParam("feeCode") String feeCode,ModelMap model) throws ValidateFailedException {
		log.info("根据费用类型主键，查找feeCode是否存在：" + feeCode );
		String isExist = "";
		//判断需要查找的主键是否为空
		if(StringUtils.isNotBlank(feeCode)) {
			TreFeeTypeVo treFeeTypeVo = treFeeTypeService.getTreFeeTypeByFeeCode(feeCode);
			if(null != treFeeTypeVo) {
				log.info("根据feeCode查找的相关记录已存在！");
				isExist = "true";
			}
		}else {
			log.error("需要查找的feedCode不能为空！");
			isExist = "true";
		}
 		return isExist;
	}
	/**
	 * 通过条件查询数据并分页
	 * @param param
	 * @return
	 */
	@RequestMapping("/treFeeTypeList")
	@ResponseBody
	public String treFeeTypeList() {
		log.info("费用类型列表");
		List<TreFeeTypeVo> treFeeTypeList = treFeeTypeService.treFeeTypeList();
		return JSONArray.fromObject(treFeeTypeList).toString();
	}
}
